package com.seisys.controller.system.login;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.seisys.controller.base.BaseController;
import com.seisys.service.system.user.UserService;
import com.seisys.util.PageData;

@Controller
public class CrossOrigin extends BaseController{
	
	@Resource(name = "userService")
	private UserService userService;
	
	@RequestMapping(value = "/user/updatePasswordx")
	@ResponseBody
	public Object updatePassWord() {
		Gson gson = new Gson();
		Map<String, Object> result = new HashMap<>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String newPassword = pd.get("PASSWORD_NEW").toString();
		String oldPassword2 = pd.get("PASSWORD_OLD").toString();
		try {
			pd = userService.findByUiId(pd);
			 // 根据ID读取
			String oldpwd1 = pd.getString("PASSWORD");
			SimpleHash oldpwd = new SimpleHash("SHA-1", pd.getString("USERNAME"), oldPassword2);
			if (oldpwd.toString().equals(oldpwd1.toString())) {
				pd.put("PASSWORD", new SimpleHash("SHA-1", pd.getString("USERNAME"), newPassword).toString());
				userService.editPwd(pd);
				result.put("msg", "成功");
				result.put("code", "200");
			} else {
				result.put("msg", "旧密码错误，请重新输入!");
				result.put("code", "201");
			}
		} catch (Exception e) {
			result.put("msg", "系统异常，稍后再试！");
			result.put("code", "202");
		}
		return "(" + gson.toJson(result) + ")";
	}
}
